﻿using System.Collections.Generic;
using smp.ButterflyCakes.Models.Data;
using smp.ButterflyCakes.Models.ViewModels.Schedule;
using smp.ButterflyCakes.Repository.Databases;
using smp.ButterflyCakes.Repository.Interfaces;
using smp.QueryDb;
using smp.QueryDb.Structure;

namespace smp.ButterflyCakes.Repository.Implementation
{
    public class ScheduleRepository : IScheduleRepository
    {
        private readonly IDbSessionFactory _sessionFactory;

        public ScheduleRepository()
        {
            _sessionFactory = new DbSessionFactory("ButterflyCakes");
        }

        public ScheduleRepository(IDbSessionFactory sessionFactory)
        {
            _sessionFactory = sessionFactory;
        }

        #region Implementation of IScheduleRepository

        public ScheduleCalendar GetSchedule(DateModel model)
        {
            List<ScheduleListItem> items;
            using(var session = _sessionFactory.Create())
            {
                var query = new Query<ScheduleListItem>()
                    .From(Db.Order)
                    .Map(x => x.Date).To(Order.Due)
                    .Map(x => x.Description).To(Order.Title)
                    .Map(x => x.OrderStatus).To(Order.Status)
                    .Where(new Filter<ScheduleListItem>(Order.Due).Is.GreaterThanOrEqualTo(model.StartOfMonth.AddDays(-7))
                               .And(Order.Due).Is.LessThanOrEqualTo(model.EndOfMonth.AddDays(7)));
                var result = session.Query(query);
                if(result == null || result.Results == null)
                {
                    items = new List<ScheduleListItem>();
                }
                else
                {
                    items = result.Results;
                }
            }
            return new ScheduleCalendar(model, items);
        }

        public ScheduleDay GetScheduleDay(DateModel model)
        {
            List<OrderSummaryListItem> items;
            using (var session = _sessionFactory.Create())
            {
                var query = new Query<OrderSummaryListItem>()
                    .From(Db.Order
                              .OuterJoin(new Join(Db.Contact).On(Order.ContactId).IsEqualTo(Contact.Id)))
                    .Map(x => x.ContactTitle).To(Order.Title)
                    .Map(x => x.ContactFirstName).To(Contact.FirstName)
                    .Map(x => x.ContactLastName).To(Contact.LastName)
                    .Map(x => x.DueDate).To(Order.Due)
                    .Map(x => x.Id).To(Order.Id)
                    .Map(x => x.Status).To(Order.Status)
                    .Map(x => x.SummaryDescription).To(Order.Summary)
                    .Map(x => x.Title).To(Order.Title)
                    .Where(new Filter<OrderSummaryListItem>(Order.Due).Is.EqualTo(model.Date));
                var result = session.Query(query);
                if (result == null || result.Results == null)
                {
                    items = new List<OrderSummaryListItem>();
                }
                else
                {
                    items = result.Results;
                }
            }
            return new ScheduleDay {Date = model.Date, Orders = items};
        }

        #endregion
    }
}
